package navigation.results;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

import org.htmlparser.Node;
import org.htmlparser.Parser;
import org.htmlparser.filters.HasAttributeFilter;
import org.htmlparser.lexer.Lexer;
import org.htmlparser.lexer.Page;
import org.htmlparser.util.NodeList;
import org.htmlparser.util.ParserException;
import org.htmlparser.util.SimpleNodeIterator;

import wish.experimental.TestingFilters;
import wish.experimental.TestingFilters.WISH;
import wish.stages.ParsingStage;

public class ResultsPageFinder {

	public class ResultsPageFinderRunnable implements Runnable{

		private File file;
		private File results;
		private HasAttributeFilter filter;

		public ResultsPageFinderRunnable(File file, File results) {
			this.file = file;
			this.results = results;
			this.filter = new HasAttributeFilter("href");
		}

		@Override
		public void run() {
			
			String htmlContent;
			
			try {
				
				htmlContent = ParsingStage.readFile(file);
				
				Parser parser = new Parser(new Lexer(new Page(htmlContent, "UTF-8")));
				
				NodeList n = parser.parse(filter);
				
				BufferedWriter bw = new BufferedWriter(new FileWriter(results));
				
				int index = 0;
				
				while (index < n.size()){
				
					Node node = n.elementAt(index);
					
					bw.write("<div id=" + index + "-" + getLevel(node) + ">");
					
					bw.write(n.elementAt(index).toHtml());
					
					bw.write("</div>");
					
					bw.write("\n <br><br><br> \n");
					
					index++;
				}
				bw.close();
				
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (ParserException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			
		}

		private int getLevel(Node node) {
			
			if (node.getParent() == null){
				return 0;
			}else{
				return getLevel(node.getParent())+1;
			}
			
		}
		
	}
	
	
	public static void main(String[] args) throws IOException, ParserException {
		
		String type = "training"; //"training","test"
		
		String out = "/proj/dbNoBackup/pjbarrio/Experiments/Wrappers/NavigationEmpty/" + type + "/";
		
		String results = "/proj/dbNoBackup/pjbarrio/Experiments/Wrappers/QueryResultsEmpty/" + type + "/";
		
		File resFolder = new File(results);
		
		File[] databaseFolders = resFolder.listFiles();
		
		for (int i = 0; i < databaseFolders.length; i++) {
			
			System.out.println("DATABASE: " + i);
			
			String name = databaseFolders[i].getName();
			
			File newfolder = new File(out + name);
			
			if (newfolder.exists())
				continue;
			
			newfolder.mkdir();
			
			File[] toWrap = databaseFolders[i].listFiles();
			
			for (int j = 0; j < toWrap.length; j++) {
				
				System.gc();
				
				System.out.println("FILE: " + j);
				
				File outputFile = new File(newfolder,toWrap[j].getName());
				
				new ResultsPageFinder().execute(toWrap[j],outputFile);
				
			}
			
		}
		
		
		
	}

	private void execute(File file, File wrapper) {
		
		Thread t = new Thread(new ResultsPageFinderRunnable(file,wrapper));
		
		t.start();
		
		try {
			t.join();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		
	}

	
}
